package com.trkj.service;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.jpa.CcBerthDAO;
import com.trkj.dao.jpa.CcDoctorAdviceDAO;
import com.trkj.dao.jpa.CcPatientDAO;
import com.trkj.dao.jpa.CcWardDAO;
import com.trkj.dao.jpa.IYzrTbstaffDAO;
import com.trkj.dao.jpa.TbdepartmentDAO;
import com.trkj.pojos.CcBerth;
import com.trkj.pojos.CcDoctorAdvice;
import com.trkj.pojos.CcPatient;
import com.trkj.pojos.CcWard;
import com.trkj.pojos.Tbdepartment;
import com.trkj.pojos.Tbstaff;
import com.trkj.vo.CcDoctorAdviceVo;

@Service
@Transactional
public class CcDoctorAdviceService {

	@Autowired
	private CcDoctorAdviceDAO ccdaDAO;
	@Autowired
	private IYzrTbstaffDAO staffDAO;
	@Autowired
	private CcPatientDAO ccpDAO;
	@Autowired
	private CcBerthDAO ccBerthDAO;
	@Autowired
	private CcWardDAO ccWardDAO;
	@Autowired
	private TbdepartmentDAO tbdeDAO;
	public Map<String, Object> queryBydoctorid(int page,int rows,String elePerid){
		PageRequest pr = new PageRequest(page - 1, rows);
		Map<String, Object> map=new HashMap<String, Object>();
		List<CcDoctorAdvice> list=ccdaDAO.queryAllCcDoctorAdvice(pr, new BigDecimal(elePerid));
		//CcDoctorAdviceToCcDoctorAdviceVo
		map.put("total", ccdaDAO.queryByCount(new BigDecimal(elePerid)));
		map.put("rows", CcDoctorAdviceToCcDoctorAdviceVo(list));
		return map;
	} 
	public void addCcDoctorAdvice(CcDoctorAdvice cda){
		CcDoctorAdvice cda2=cda;
		cda2.setAdName(cda.getAdName());
		cda2.setElePerid(cda.getElePerid());
		cda2.setPrintdate(new Date());
		cda2.setPrintstaff( cda.getPrintstaff());
		ccdaDAO.save(cda2);
	}
	public boolean updateCcDoctorAdvice(CcDoctorAdvice cda){
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(currentTime);
		
		CcDoctorAdvice cda2=ccdaDAO.queryByOne(cda.getDaId());
		String dateString2 = formatter.format(cda2.getPrintdate());
		if(cda2!=null&&dateString2.equals(dateString)){
			cda2.setAdName(cda.getAdName());
			cda2.setElePerid(cda.getElePerid());
			ccdaDAO.save(cda2);
			return true;
		}else{
			return false;
		}
		
	}
	public List<CcDoctorAdviceVo> queryByPname(String pname){
		List<CcPatient> list =ccpDAO.findBypname(pname);
		List<CcDoctorAdviceVo> listVo=new ArrayList<CcDoctorAdviceVo>();
		for(CcPatient cp:list){
			List<CcDoctorAdvice> ccda=ccdaDAO.queryByelePerid(new BigDecimal(cp.getPatientid()));
			listVo=CcDoctorAdviceToCcDoctorAdviceVo(ccda);
		}
		return listVo;
	}
	public void deleteCDA(long [] list){
		for(int i=0;i<list.length;i++){
			CcDoctorAdvice cda2=ccdaDAO.queryByOne(list[i]);
			Date currentTime = new Date();
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			String dateString = formatter.format(currentTime);
			String dateString2 = formatter.format(cda2.getPrintdate());
			if(dateString.equals(dateString2)){
				ccdaDAO.delete(cda2);
			}
		}
	}
	public CcDoctorAdvice queryOne(long id){
		return ccdaDAO.findOne(id);
	}
	private List<CcDoctorAdviceVo> CcDoctorAdviceToCcDoctorAdviceVo(List<CcDoctorAdvice> list){
		List<CcDoctorAdviceVo> listVo=new ArrayList<CcDoctorAdviceVo>();
		for(CcDoctorAdvice cda:list){
			CcDoctorAdviceVo cdaVo=new CcDoctorAdviceVo();
			cdaVo.setAdName(cda.getAdName());
			cdaVo.setDaId(cda.getDaId());
			cdaVo.setElePerid(cda.getElePerid());
			cdaVo.setPrintdate(cda.getPrintdate());
			cdaVo.setPrintstaff(cda.getPrintstaff());
			CcPatient cp=ccpDAO.findByOneCcPatientid(cdaVo.getElePerid().longValue());
			if(cp!=null){
				cdaVo.setPid(cp.getPatientid());
				cdaVo.setPname(cp.getPname());
				if(cp.getPaberthid()!=null){
					CcBerth cb=ccBerthDAO.findOne(cp.getPaberthid().longValue());
					if(cb!=null){
						CcWard cw= ccWardDAO.findOne(cb.getBwardno().longValue());
						cdaVo.setGid(cw.getWardid());
						cdaVo.setGename(cw.getWname());
					}
				}
				if(cp.getPofficesid()!=null){
					Tbdepartment off=tbdeDAO.findOne(cp.getPofficesid().longValue());
					if(off!=null){
						cdaVo.setDepartmentid(off.getDepartmentNo());
						cdaVo.setDepartmentName(off.getDepartmentName());
					}
				}
			}
			Tbstaff staff=staffDAO.findOne(cdaVo.getPrintstaff().longValue());
			if(staff!=null){
				cdaVo.setStaffid(staff.getStaffNo());
				cdaVo.setStaffName(staff.getStaffName());
			}
			listVo.add(cdaVo);
		}
		
		return listVo;
	}
}
